package com.itheima.health.controller;

import com.itheima.interfaces.CheckItemService;
import com.itheima.pojo.CheckItem;
import com.itheima.resources.MessageConstant;
import com.itheima.resources.entity.QueryPageBean;
import com.itheima.resources.entity.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("checkitem")
public class CheckItemController {

    @Reference //引入dubbo对应的注解
    private CheckItemService checkItemService;

    @RequestMapping("add.do")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            //   checkItem  有无 ID     如果实体类存在 id   update 操作 否则 插入操作
            checkItemService.saveOrUpdate(checkItem);
            return new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_CHECKITEM_FAIL);
        }
    }

    //  有条件和无条件查询 都是同一个方法
    @RequestMapping("findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            Map map = checkItemService.findPageByContion(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    //逻辑删除
    @RequestMapping("deleteCheckItemById.do")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')") //   表示拥有 ROLE_ADMIN角色的用户才可以执行该方法
    public Result deleteCheckItemById(@RequestParam("id") Integer id) {
        try {
            //  update  xxx  set is_delete = 1 where id = 1
            CheckItem checkItem = new CheckItem();
            checkItem.setId(id);
            checkItem.setIs_delete(1); //  修改字段值
            checkItemService.updateById(checkItem);
            return new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_CHECKITEM_FAIL);
        }

    }

    //查询检查项多选框回显
    @RequestMapping("findAll")
    public Result findAll() {
        try {
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //检查组编辑已选检查项查询
    @RequestMapping("findCheckItemIdsByGroupId.do")
    public Result findCheckItemIdsByGroupId(@RequestParam("id") int id) {
        try {
            List<Integer> list = checkItemService.findCheckItemIdsByGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
